package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.ProductType;
import com.windfone.service.ProductTypeService;

public class ProductTypeAction  extends ActionSupport {
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private ProductTypeService productTypeService;
	/** page avaible area */
	private List<ProductType> productTypeList;
	private int productTypeId;
	private int langId;
	private String typeName;
	private String typenameEnglish;
	private String imagepath;
	
	private ProductType bean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	
	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	public String getTypenameEnglish() {
		return typenameEnglish;
	}

	public String getImagepath() {
		return imagepath;
	}

	public void setTypenameEnglish(String typenameEnglish) {
		this.typenameEnglish = typenameEnglish;
	}

	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}

	public ProductType getBean() {
		return bean;
	}

	public int getLangId() {
		return langId;
	}

	public void setLangId(int langId) {
		this.langId = langId;
	}

	public int getProductTypeId() {
		return productTypeId;
	}

	public void setProductTypeId(int productTypeId) {
		this.productTypeId = productTypeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public void setBean(ProductType bean) {
		this.bean = bean;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public List<ProductType> getProductTypeList() {
		return productTypeList;
	}

	public void setProductTypeList(List<ProductType> productTypeList) {
		this.productTypeList = productTypeList;
	}

	public int getBeginRecord() {
		return beginRecord;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

 
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addProductType()throws Exception {
		ProductType bean = new ProductType();
		bean.setTypeName(typeName);
		bean.setLangId(langId);
		bean.setImagepath(imagepath);
		bean.setTypenameEnglish(typenameEnglish);
		productTypeService.insertProductType(bean);
		return SUCCESS;
	}
	
	public String deleteProductType()throws Exception {
		productTypeService.deleteProductType(productTypeId);
		return SUCCESS;
	}
	
	public String beforUpdateProductType()throws Exception {
		bean = productTypeService.getProductTypeId(productTypeId).get(0);
		return SUCCESS;
	} 
	
	public String updateProductType()throws Exception {
		ProductType bean = new ProductType();
		bean.setTypeName(typeName);
		bean.setLangId(langId);
		bean.setId(productTypeId);
		bean.setTypenameEnglish(typenameEnglish);
		bean.setImagepath(imagepath);
		productTypeService.updateProductType(bean);
		return SUCCESS;
	}
	
	public String productTypeList() throws Exception{
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = productTypeService.getAllRecorderNumber() ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		productTypeList = productTypeService.getAllProductType(beginRecord);
		return SUCCESS;
	}
}
